package com.hna.face.controller;

import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.hna.face.config.SysConfig;
import com.hna.face.model.FaceRecognizeResult;
import com.hna.face.mqtt.MqttService;


@RestController
@RequestMapping(value = "/faceApi")
public class FaceServiceController {

	private final static Logger log = LoggerFactory.getLogger(FaceServiceController.class);

	@Autowired
	MqttService mqttService;
	
//	@Scheduled(cron="${face-sync-schedule}",zone="${time-zone}")
//	private void willPublish() throws MqttPersistenceException, MqttException {
//		log.info("willPublish start");
//		MqttMessage mqttMessage = new MqttMessage();
//		log.info("mqttMessage:"+mqttMessage);
//		mqttMessage.setPayload(JSON.toJSONString(SysConfig.mqttClientid+ "close").getBytes());
//		mqttMessage.setQos(1);
//		mqttMessage.setRetained(true);
//		mqttService.publish(mqttMessage,SysConfig.mqttWill);
//		log.info("willPublish end");
//	}
//	
//	@Scheduled(cron="${face-sync-schedule}",zone="${time-zone}")
//	private void onlinePublish() throws MqttPersistenceException, MqttException {
//		log.info("onlinePublish start");
//		MqttMessage mqttMessage = new MqttMessage();
//		mqttMessage.setPayload(JSON.toJSONString(SysConfig.mqttClientid + SysConfig.mqttOnline).getBytes());
//		mqttMessage.setQos(1);
//		mqttMessage.setRetained(true);
//		mqttService.publish(mqttMessage,SysConfig.mqttOnline);
//		log.info("onlinePublish end");
//	}
//	
//	@Scheduled(cron="${face-sync-schedule}",zone="${time-zone}")
//	private void dataPublish() throws MqttPersistenceException, MqttException {
//		log.info("dataPublish start");
//		FaceRecognizeResult result = new FaceRecognizeResult();
//	    result.setVehicleNo(SysConfig.mqttClientid);
//	    result.setResult(1);
//	    result.setPersonName("张三");
//	    result.setPhoto("1111111111");
//		MqttMessage mqttMessage = new MqttMessage();
//		mqttMessage.setPayload(JSON.toJSONString(result).getBytes());
//		mqttMessage.setQos(1);
//		mqttMessage.setRetained(true);
//		mqttService.publish(mqttMessage,SysConfig.mqttData);
//		log.info("dataPublish end");
//	}
	
	@RequestMapping("publish")
	private void devicePublish(MqttMessage message,String topic) {
		try {
			mqttService.publish(message, topic);
		} catch (MqttException e) {
			log.error("publish:",e);
		}
	}
	
	@RequestMapping("subscribe")
	private void deviceSubscribe(int qos,String topic) {
		try {
			mqttService.subscribe(qos, topic);
		} catch (MqttException e) {
			log.error("subscribe:",e);
		}
	}
}